<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>08_计算器练习</title>
</head>
<body>
    <input type="text" id="num1" placeholder="请输入数字1"> <br>
    <input type="text" id="num2" placeholder="请输入数字2"> <br>
    <button onclick="f1('+')">加</button>
    <button onclick="f1('-')">减</button>
    <button onclick="f1('*')">乘</button>
    <button onclick="f1('/')">除</button>
    <h2>运算结果：<span></span></h2>

<script>
  function f1(op){
    var a = parseFloat(num1.value);
    var b = parseFloat(num2.value);
    var c;
    switch (op){
      case '+':
        c = a+b;
        break;
      case '-':
        c = a-b;
        break;
      case '*':
        c = a*b;
        break;
      case '/':
        if (b==0){
          c = "除数不能为0";
          break;
        }
        c = a/b;
        break;
      default:
        c = "输入有误";
    }
    document.querySelector("span").innerHTML = c;
  }
</script>
</body>
</html>